
import { createSlice, PayloadAction, createAsyncThunk } from '@reduxjs/toolkit'
import api from '@/api'
import Taro from '@tarojs/taro'

const initialState = {
  userInfo: {},
  messageNum: '',
  today_data: {},

}

export const fetchUserFavs = createAsyncThunk('user/fetchUserFavs', async (params) => {
  console.log(2222)
})

const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    updateUserInfo: (state, { payload }) => {
      const { memberInfo} =
        payload
      state.userInfo = {
        ...memberInfo,
      }
    },

    updateMessageNum: (state, { payload }) => {
      state.messageNum = payload
      console.log('=======', payload)
      if (payload > 0) {
        console.log('=======1', payload)
        Taro.setTabBarBadge({
          index: 1,
          text: payload
        })
      } else {
        Taro.removeTabBarBadge({
          index: 1
        })
      }

    },
    updateToday_data: (state, { payload }) => {
      state.today_data = payload
    },

    clearUserInfo: (state, { payload }) => {
      state.userInfo = null
      state.messageNum = ''
      state.today_data = ''
    },
  },
})

export const { updateUserInfo, updateMessageNum, updateToday_data, clearUserInfo } =
  userSlice.actions

export default userSlice.reducer